/**
 * Created by Yang on 2014/10/14.
 */
var cache = module.exports = {};
var path = require('path');
var Datastore = require('nedb');

var cacheFile = path.join(__dirname + '../../tmp/cache.db');
var db = new Datastore({ filename: cacheFile, autoload: true });
var log = console;
/*
*TODO
* 1.性能测试
* 2.自动清理
* */

cache.get = function(key,callback){
    log.info(key);
    db.findOne({myKey:key},function(err,doc){
        if(callback){
            callback(err,doc);
        }
    });
}

cache.set = function(key,value,callback){
    db.insert({myKey:key,value:value},function(err,doc){
        log.info(doc);
        if(callback){
            callback(err,doc);
        }
    });
}

cache.reload = function(){
    db = new Datastore({ filename: cacheFile, autoload: true });
}
